<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class km_supplier extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('km_supplier_model', 'Supplier');
		$this->load->model('combo_model', 'Combo');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup Supplier";
		
		// Define Combo Data
		$data['arr_application']	= $this->Combo->ITApplication();
		
		viewByTemplate('km_supplier/index', $data, 'grid');
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->Supplier->GetJSON();
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		//supplier_id, supplier_kode, supplier_nama, supplier_alamat, supplier_telp, supplier_fax, supplier_email, supplier_website, supplier_npwp, 
		//supplier_npwp_pengukuhan, supplier_siup, supplier_situ, supplier_ijin_depkes, supplier_ijin_date, supplier_pkp, supplier_nokontrak, 
		//supplier_kontrak_awal, supplier_kontrak_akhir, supplier_konsinyasi, supplier_aktif, supplier_jatuh_tempo
		if (post("supplier_kode") == '') {
			$error->Add("supplier_kode", "Kode Supplier", $tpl_empty);
		} else if (post("supplier_nama") == '') {
			$error->Add("supplier_nama", "Nama Supplier", $tpl_empty);
		} else if (post("supplier_alamat") == '') {
			$error->Add("supplier_alamat", "Alamat Supplier", $tpl_empty);
		} else if (post("supplier_telp") == '') {
			$error->Add("supplier_telp", "Telp Supplier", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM km_supplier WHERE supplier_kode = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("supplier_kode"))))."'")) > 0) {
			$error->Add("supplier_kode", "Kode Supplier", $tpl_duplicate);
		}		
		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	= post_by_key(array('supplier_kode','supplier_nama','supplier_alamat','supplier_telp','supplier_fax','supplier_email','supplier_website','supplier_npwp,supplier_npwp_pengukuhan','supplier_siup','supplier_situ','supplier_ijin_depkes','supplier_ijin_date','supplier_pkp','supplier_nokontrak','supplier_kontrak_awal','supplier_kontrak_akhir','supplier_konsinyasi','supplier_aktif','supplier_jatuh_tempo'));
				$data['supplier_nama']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['supplier_nama'])));
				$data['supplier_kode']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['supplier_kode'])));
				
				if (post("mode") == "create")
				{
					$this->Supplier->Add($data);
					$ls_response = "Data Tersimpan";

				}
				else if (post("mode") == "edit")
				{
					$this->Supplier->Update($data, array('supplier_kode'	=> $data['supplier_kode']));
					$ls_response = "Data Tersimpan";
				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$supplier_kode	= get('supplier_kode');
		
		if ($supplier_kode != "")
		{
			// Delete Single
			$arr_rule	= array('supplier_kode' => $supplier_kode);
			$ls_result = $this->Supplier->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('supplier_kode' => $row['supplier_kode']);
				$ls_result .= $this->Supplier->DeleteSingle($arr_rule);
			}
		}
		
		echo $ls_result;
	}
}
?>